package com.navinfo.tripanalysis.common.arithmetic.convert;

import com.navinfo.tripanalysis.common.arithmetic.common.OuterEventData;
import com.navinfo.tripanalysis.common.protocol.java.EventDataPb;
import org.bson.Document;

/**
 * 事件数据与mongo格式转换
 *
 * @author 沈东生
 */
public class EventDataMongoConvert {
    /**
     * 对像转换为Document
     *
     * @param m
     * @return
     */
    public static Document toDocument(OuterEventData m) {
        Document doc = new Document();
        /*--start_schema*/
        if(m.getUid()!=null){ doc.put("uid",m.getUid());}
        if(m.getTid()!=null){ doc.put("terminalId",m.getTid());}
        if(m.getStype()!=null){ doc.put("stype",m.getStype());}
        if(m.getStartGpsTime()!=null){ doc.put("startGpsTime",m.getStartGpsTime());}
        if(m.getStartLatitude()!=null){ doc.put("startLatitude",m.getStartLatitude());}
        if(m.getStartLongitude()!=null){ doc.put("startLongitude",m.getStartLongitude());}
        if(m.getStartHeight()!=null){ doc.put("startHeight",m.getStartHeight());}
        if(m.getStartSpeed()!=null){ doc.put("startSpeed",m.getStartSpeed());}
        if(m.getStartGpsDirection()!=null){ doc.put("startGpsDirection",m.getStartGpsDirection());}
        if(m.getStopGpsTime()!=null){ doc.put("stopGpsTime",m.getStopGpsTime());}
        if(m.getStopLatitude()!=null){ doc.put("stopLatitude",m.getStopLatitude());}
        if(m.getStopLongitude()!=null){ doc.put("stopLongitude",m.getStopLongitude());}
        if(m.getStopHeight()!=null){ doc.put("stopHeight",m.getStopHeight());}
        if(m.getStopSpeed()!=null){ doc.put("stopSpeed",m.getStopSpeed());}
        if(m.getStopGpsDirection()!=null){ doc.put("stopGpsDirection",m.getStopGpsDirection());}
        if(m.getDuration()!=null){ doc.put("duration",m.getDuration());}
//          EventDataPb.EventType event -->
        if(m.getExtendField01()!=null){ doc.put("extendField01",m.getExtendField01());}
        if(m.getExtendField02()!=null){ doc.put("extendField02",m.getExtendField02());}
        if(m.getExtendField03()!=null){ doc.put("extendField03",m.getExtendField03());}
        if(m.getExtendField04()!=null){ doc.put("extendField04",m.getExtendField04());}
        if(m.getExtendField05()!=null){ doc.put("extendField05",m.getExtendField05());}
        if(m.getExtendField06()!=null){ doc.put("extendField06",m.getExtendField06());}
        if(m.getExtendField07()!=null){ doc.put("extendField07",m.getExtendField07());}
        if(m.getExtendField08()!=null){ doc.put("extendField08",m.getExtendField08());}
        if(m.getExtendField09()!=null){ doc.put("extendField09",m.getExtendField09());}
        if(m.getExtendField10()!=null){ doc.put("extendField10",m.getExtendField10());}
        /*--end_schema*/
        doc.put("day", m.getDay());
        if(m.getEvent()!=null){
            doc.put("event",m.getEvent().name());
        }
        return doc;
    }

    /**
     * Document对象转为Pojo对象
     *
     * @param doc
     * @return
     */
    public static OuterEventData toObj(Document doc) {
        OuterEventData m = new OuterEventData();

        /*--start_row*/
        if(doc.containsKey("uid")){ m.setUid(doc.getLong("uid"));}
        if(doc.containsKey("terminalId")){ m.setTid(doc.getLong("terminalId"));}
        if(doc.containsKey("stype")){ m.setStype(doc.getInteger("stype"));}
        if(doc.containsKey("startGpsTime")){ m.setStartGpsTime(doc.getLong("startGpsTime"));}
        if(doc.containsKey("startLatitude")){ m.setStartLatitude(doc.getInteger("startLatitude"));}
        if(doc.containsKey("startLongitude")){ m.setStartLongitude(doc.getInteger("startLongitude"));}
        if(doc.containsKey("startHeight")){ m.setStartHeight(doc.getInteger("startHeight"));}
        if(doc.containsKey("startSpeed")){ m.setStartSpeed(doc.getInteger("startSpeed"));}
        if(doc.containsKey("startGpsDirection")){ m.setStartGpsDirection(doc.getInteger("startGpsDirection"));}
        if(doc.containsKey("stopGpsTime")){ m.setStopGpsTime(doc.getLong("stopGpsTime"));}
        if(doc.containsKey("stopLatitude")){ m.setStopLatitude(doc.getInteger("stopLatitude"));}
        if(doc.containsKey("stopLongitude")){ m.setStopLongitude(doc.getInteger("stopLongitude"));}
        if(doc.containsKey("stopHeight")){ m.setStopHeight(doc.getInteger("stopHeight"));}
        if(doc.containsKey("stopSpeed")){ m.setStopSpeed(doc.getInteger("stopSpeed"));}
        if(doc.containsKey("stopGpsDirection")){ m.setStopGpsDirection(doc.getInteger("stopGpsDirection"));}
        if(doc.containsKey("duration")){ m.setDuration(doc.getLong("duration"));}
//          EventDataPb.EventType event -->
        if(doc.containsKey("extendField01")){ m.setExtendField01(doc.getInteger("extendField01"));}
        if(doc.containsKey("extendField02")){ m.setExtendField02(doc.getInteger("extendField02"));}
        if(doc.containsKey("extendField03")){ m.setExtendField03(doc.getInteger("extendField03"));}
        if(doc.containsKey("extendField04")){ m.setExtendField04(doc.getInteger("extendField04"));}
        if(doc.containsKey("extendField05")){ m.setExtendField05(doc.getDouble("extendField05"));}
        if(doc.containsKey("extendField06")){ m.setExtendField06(doc.getDouble("extendField06"));}
        if(doc.containsKey("extendField07")){ m.setExtendField07(doc.getDouble("extendField07"));}
        if(doc.containsKey("extendField08")){ m.setExtendField08(doc.getDouble("extendField08"));}
        if(doc.containsKey("extendField09")){ m.setExtendField09(doc.getLong("extendField09"));}
        if(doc.containsKey("extendField10")){ m.setExtendField10(doc.getLong("extendField10"));}
        /*--end_row*/
        m.setDay(doc.getString("day"));
        if(doc.containsKey("event")){
            String eventName=doc.getString("event");
            m.setEvent(EventDataPb.EventType.valueOf(eventName));
        }
        return m;
    }
}
